package com.yykj.hospital.server.service.consultation.impl;

import com.yykj.base.client.dto.template.ConsultationTemplateDTO;
import com.yykj.base.client.dto.template.ConsultationTemplateFieldDTO;
import com.yykj.base.client.dto.template.FormFieldDTO;
import com.yykj.base.client.feign.BaseFeignClient;
import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.LocalObjectUtil;
import com.yykj.hospital.client.dto.consultation.template.HospitalConsultationDTO;
import com.yykj.hospital.client.dto.consultation.template.HospitalConsultationDetailDTO;
import com.yykj.hospital.client.dto.consultation.template.HospitalConsultationListDTO;
import com.yykj.hospital.server.model.entity.consultation.template.HospitalConsultation;
import com.yykj.hospital.server.model.entity.consultation.template.HospitalConsultationDetail;
import com.yykj.hospital.server.model.repository.consultation.template.HospitalConsultationDetailRepository;
import com.yykj.hospital.server.model.repository.consultation.template.HospitalConsultationRepository;
import com.yykj.hospital.server.service.consultation.HospitalConsultationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class HospitalConsultationServiceImpl implements HospitalConsultationService {
    @Override
    public HospitalConsultationDTO editHospitalConsultation(HospitalConsultationDTO dto) {
        HospitalConsultation hospitalConsultation;
        if (dto.getId() != null && dto.getId() > 0) {
            hospitalConsultation = hospitalConsultationRepository.findById(dto.getId()).orElseThrow(() -> new NormalException(ResultEnums.NOT_EXIST));
            LocalObjectUtil.copyNullValue(dto, hospitalConsultation);
        } else {
            hospitalConsultation = new HospitalConsultation();
            BeanUtils.copyProperties(dto, hospitalConsultation);
        }
        hospitalConsultation = hospitalConsultationRepository.save(hospitalConsultation);
        BeanUtils.copyProperties(hospitalConsultation, dto);
        return dto;
    }

    @Override
    public HospitalConsultationDetailDTO editHospitalConsultationDetail(HospitalConsultationDetailDTO detailDTO) {
        HospitalConsultationDetail detail;
        if (detailDTO.getId() != null && detailDTO.getId() > 0) {
            detail = hospitalConsultationDetailRepository.findById(detailDTO.getId()).orElseThrow(() -> new NormalException(ResultEnums.NOT_EXIST));
        } else {
            detail = new HospitalConsultationDetail();
        }
        LocalObjectUtil.copyNullValue(detailDTO, detail);
        detail = hospitalConsultationDetailRepository.save(detail);
        BeanUtils.copyProperties(detail, detailDTO);
        return detailDTO;
    }

    @Override
    public void deleteHospitalConsultationDetail(Integer id) {
        hospitalConsultationDetailRepository.deleteById(id);
    }

    @Override
    public List<HospitalConsultationListDTO> listHospitalConsultation(Integer hospitalId) {
        List<HospitalConsultation> hospitalConsultationList = hospitalConsultationRepository.findAllByHospitalIdOrderBySorting(hospitalId);
        if (hospitalConsultationList == null || hospitalConsultationList.size() == 0) {
            this.initHospitalConsultation(hospitalId);
            hospitalConsultationList = hospitalConsultationRepository.findAllByHospitalIdOrderBySorting(hospitalId);
        }

        List<HospitalConsultationDetail> detailList = hospitalConsultationDetailRepository.findAllByHospitalId(hospitalId);
        Map<Integer, List<HospitalConsultationDetailDTO>> detailMap = new HashMap<>();
        List<HospitalConsultationDetailDTO> list;
        for (HospitalConsultationDetail detail : detailList) {
            list = detailMap.getOrDefault(detail.getConsultationId(), new ArrayList<>());
            HospitalConsultationDetailDTO detailDTO = new HospitalConsultationDetailDTO();
            BeanUtils.copyProperties(detail,detailDTO);
            list.add(detailDTO);
            detailMap.put(detail.getConsultationId(),list);
        }

        List<HospitalConsultationListDTO> result = new ArrayList<>();
        for (HospitalConsultation hospitalConsultation : hospitalConsultationList) {

            HospitalConsultationListDTO listDTO = new HospitalConsultationListDTO();
            BeanUtils.copyProperties(hospitalConsultation,listDTO);
            listDTO.setList(detailMap.get(hospitalConsultation.getId()));
            result.add(listDTO);
        }
        return result;
    }

    @Override
    public List<HospitalConsultationDetailDTO> listConsultationFields(Integer consultationId) {
        List<HospitalConsultationDetail> detailList = hospitalConsultationDetailRepository.findAllByConsultationId(consultationId);
        List<HospitalConsultationDetailDTO> retList = new ArrayList<>();
        for (HospitalConsultationDetail detail : detailList) {
            HospitalConsultationDetailDTO detailDTO = new HospitalConsultationDetailDTO();
            BeanUtils.copyProperties(detail,detailDTO);
            retList.add(detailDTO);
        }
        return retList;
    }

    public void initHospitalConsultation(Integer hospitalId) {
        List<FormFieldDTO> commonFormFieldList = baseFeignClient.commonFormFieldList();
        List<ConsultationTemplateDTO> consultationTemplateList = baseFeignClient.listConsultationTemplate();

        for (ConsultationTemplateDTO consultationTemplate : consultationTemplateList) {
            List<HospitalConsultationDetail> detailList = new ArrayList<>();
            HospitalConsultation hospitalConsultation = new HospitalConsultation();
            BeanUtils.copyProperties(consultationTemplate, hospitalConsultation);
            hospitalConsultation.setHospitalId(hospitalId);
            hospitalConsultation.setId(null);
            hospitalConsultation = hospitalConsultationRepository.save(hospitalConsultation);
            List<ConsultationTemplateFieldDTO> templateFieldList = baseFeignClient.listConsultationTemplateFields(consultationTemplate.getId());
            for (FormFieldDTO formFieldDTO : commonFormFieldList) {
                HospitalConsultationDetail detail = new HospitalConsultationDetail();
                BeanUtils.copyProperties(formFieldDTO, detail);
                detail.setConsultationId(hospitalConsultation.getId());
                detail.setId(null);
                detail.setHospitalId(hospitalId);
                detailList.add(detail);
            }

            for (ConsultationTemplateFieldDTO templateField : templateFieldList) {
                HospitalConsultationDetail detail = new HospitalConsultationDetail();
                BeanUtils.copyProperties(templateField, detail);
                detail.setConsultationId(hospitalConsultation.getId());
                detail.setId(null);
                detail.setHospitalId(hospitalId);
                detailList.add(detail);
            }
            Collections.sort(detailList);
            hospitalConsultationDetailRepository.saveAll(detailList);
        }
    }

    private BaseFeignClient baseFeignClient;

    private HospitalConsultationRepository hospitalConsultationRepository;

    private HospitalConsultationDetailRepository hospitalConsultationDetailRepository;


    @Autowired
    public void setHospitalConsultationRepository(HospitalConsultationRepository hospitalConsultationRepository) {
        this.hospitalConsultationRepository = hospitalConsultationRepository;
    }

    @Autowired
    public void setHospitalConsultationDetailRepository(HospitalConsultationDetailRepository hospitalConsultationDetailRepository) {
        this.hospitalConsultationDetailRepository = hospitalConsultationDetailRepository;
    }

    @Autowired
    public void setBaseFeignClient(BaseFeignClient baseFeignClient) {
        this.baseFeignClient = baseFeignClient;
    }
}
